/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.button;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.SwingConstants;
import javax.swing.text.JTextComponent;

import org.jxSwing.i18n.I18N;
import org.jxUtils.image.ImageUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.button
 * @date 03/03/2014 - 10:23:22
 */
public class ForeColorButton extends JButton {
	
	private static final long serialVersionUID = 2407332002615877081L;
	
	private final JTextComponent textComponent;
	
	private final BufferedImage image = ImageUtil.newBufferedImage(20, 5);
	
	/**
	 * @param textComponent
	 */
	public ForeColorButton(final JTextComponent textComponent) {
	
		super();
		
		this.textComponent = textComponent;
		
		initGUI();
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		setText("F");
		setToolTipText(I18N.font().foreground());
		setSize(new Dimension(25, 25));
		setPreferredSize(new Dimension(25, 25));
		setMinimumSize(new Dimension(25, 25));
		setMaximumSize(new Dimension(25, 25));
		setFont(new Font("Dialog", Font.BOLD, 12));
		
		//
		setSelectedColor(getTextComponent().getForeground());
		setHorizontalTextPosition(SwingConstants.CENTER);
		setVerticalTextPosition(SwingConstants.TOP);
		setIconTextGap(0);
		
		addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(final ActionEvent actionEvent) {
			
				final Color color = JColorChooser.showDialog(getTextComponent(), getToolTipText(), getForeground());
				
				//
				if(color != null) {
					
					setSelectedColor(color);
					
					//
					if(getTextComponent().getText().isEmpty()) {
						
						getTextComponent().setText(" ");
						getTextComponent().setForeground(color);
						getTextComponent().setText("");
						
					}else {
						
						getTextComponent().setForeground(color);
					}
				}
			}
		});
	}
	
	/**
	 * @return the color
	 */
	public Color getSelectedColor() {
	
		return image.getGraphics().getColor();
	}
	
	/**
	 * @return the textComponent
	 */
	public JTextComponent getTextComponent() {
	
		return textComponent;
	}
	
	/**
	 * @param color
	 */
	public void setSelectedColor(final Color color) {
	
		setIcon(new ImageIcon(ImageUtil.newBufferedImage(20, 5, color)));
	}
}
